//
//  DailyTalkVC.m
//  Cunli
//
//  Created by lilubin on 15/6/18.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "DailyTalkVC.h"
#import "NewsCell.h"
#import "NSString+MD5.h"
#import "MyDataService.h"
#import "NewsModel_yy.h"
#import "NewDetailController.h"
#import "UIViewExt.h"
#import "SwipableViewController.h"
#import "DailyTalkNewsVC.h"
#import "Config.h"
#import "UIColor+expanded.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define pageSize 6

static NSString *kNewsCellID = @"NewsCell";

@interface DailyTalkVC ()
{
    NSMutableArray *_allDataModelArray; //法，谈，笑，三个部分所有model数据
    NSMutableArray *_mryfDataModelArray; //每日一法model数据
    NSMutableArray *_mrytDataModelArray; //每日一谈model数据
    NSMutableArray *_mryxDataModelArray; //每日一笑model数据
    NSArray *_dailyTitles; //每日一法，每日一谈，每日一笑
}

@property (nonatomic, strong) SwipableViewController *dailyTalkDetailVC;

@end

@implementation DailyTalkVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAllAttributes];
    [self loadNewsData];
    [self registerNewsCell];
//    self.tableView.backgroundColor = [UIColor colorWithHexString:InfoCellTitleColorValue];
    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.separatorColor = [UIColor colorWithHexString:TableViewSeperateColorValue];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initAllAttributes
{
    self.tableView.bounces = NO;
    _mryfDataModelArray = [NSMutableArray arrayWithCapacity:3];
    _mrytDataModelArray = [NSMutableArray arrayWithCapacity:3];
    _mryxDataModelArray = [NSMutableArray arrayWithCapacity:3];
    _allDataModelArray = [NSMutableArray arrayWithCapacity:9];
    
    DailyTalkNewsVC *dailyLawVC = [[DailyTalkNewsVC alloc] initWithDailyTalkType:DailyTalkTypeLaw];
    DailyTalkNewsVC *dailyTalkVC = [[DailyTalkNewsVC alloc] initWithDailyTalkType:DailyTalkTypeTalk];
    DailyTalkNewsVC *dailySmileVC = [[DailyTalkNewsVC alloc] initWithDailyTalkType:DailyTalkTypeSmile];
    
    NSMutableArray *dailyVCs = [NSMutableArray arrayWithObjects:dailySmileVC, dailyTalkVC, dailyLawVC, nil];
    _dailyTalkDetailVC = [[SwipableViewController alloc] initWithTitle:@"每日谈" andSubTitles:@[@"每日一笑", @"每日一谈", @"每日一法"] andControllers:dailyVCs  underTabbar:NO];
    _dailyTitles = [NSArray arrayWithObjects:@"每日一笑", @"每日一谈", @"每日一法", nil];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _allDataModelArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = [NSArray arrayWithArray:_allDataModelArray[section]];
    return array.count+1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = [NSArray arrayWithArray:_allDataModelArray[indexPath.section]];
    if (indexPath.row == array.count) {
        return 30;
    }
    return 90;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    sectionBgView.backgroundColor = [UIColor whiteColor];
    
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 80, 30)];
    [leftLabel setTextColor:[UIColor colorWithHexString:InfoCellIntroduceColorValue]];
    [leftLabel setFont:[UIFont systemFontOfSize:14]];
    leftLabel.text = _dailyTitles[section];
    [sectionBgView addSubview:leftLabel];
    
    /*
    UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    headerButton.frame = CGRectMake(kScreenWidth-60, 0, 60, 30);
    [headerButton setTitle:@"更多" forState:UIControlStateNormal];
    [headerButton setTitleColor:[UIColor colorWithHexString:@"0x222222"] forState:UIControlStateNormal];
    [headerButton setTintColor:[UIColor blackColor]];
    [headerButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    headerButton.tag = 200+section; //用于后面判断跳到每日谈的哪一页
    [sectionBgView addSubview:headerButton];
    */
    return sectionBgView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *sectionBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
    sectionBgView.backgroundColor = [UIColor colorWithHexString:UIBgColorValue];
    return sectionBgView;
}


- (void)buttonAction:(UIButton *)button
{
    [_dailyTalkDetailVC scrollToViewAtIndex:button.tag-200];
    _dailyTalkDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:_dailyTalkDetailVC animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *array = [NSArray arrayWithArray:_allDataModelArray[indexPath.section]];
    if (indexPath.row == array.count)
    {
        UITableViewCell *moreCell = [UITableViewCell new];
        [moreCell setBackgroundColor:[UIColor whiteColor]];
        UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [moreBtn setFrame:CGRectMake((kScreenWidth*1.0/2.0)-50, 4, 100, 21)];
        [moreBtn setTitle:@"查看更多" forState:UIControlStateNormal];
        [moreBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [moreCell.contentView addSubview:moreBtn];
        [moreBtn setTitleColor:[UIColor colorWithHexString:InfoCellIntroduceColorValue] forState:UIControlStateNormal];
        moreBtn.tag = 200+indexPath.section;
        [moreBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        moreCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return moreCell;
    }
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:kNewsCellID forIndexPath:indexPath];
    cell.newsModel = _allDataModelArray[indexPath.section][indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSArray *array = [NSArray arrayWithArray:_allDataModelArray[indexPath.section]];
    if (indexPath.row == array.count) {
        return;
    }
    NewDetailController *newDetailVC = [[NewDetailController alloc] init];
    NewsModel_yy *newModel2 = _allDataModelArray[indexPath.section][indexPath.row];
    newDetailVC.itemID = newModel2.itemid;
    newDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:newDetailVC animated:YES];
}


- (void)registerNewsCell
{
    UINib *nib = [UINib nibWithNibName:kNewsCellID bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:kNewsCellID];
}

- (void)loadNewsData
{
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:5];
    
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:@"4" forKey:@"type"];
    [CheckParams setObject:@"1" forKey:@"page"];
    [CheckParams setObject:@"6" forKey:@"pagesize"];
    [CheckParams setObject:[Config getOwnAreaids] forKey:@"areaids"];
    [CheckParams setObject:@"time_down" forKey:@"orderby"];
    
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"newslist.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSString *response = [result objectForKey:@"response"];
        if ([response isEqualToString:@"newslist"])
        {
            NSDictionary *newslistDic = result[@"newslist"];
            NSArray *mryfArray = newslistDic[@"mryf_artlist"]; //每日一法
            NSArray *mrytArray = newslistDic[@"mryt_artlist"]; //每日一谈
            NSArray *mryxArray = newslistDic[@"mryx_artlist"]; //每日一笑
            
            for(NSDictionary *mryfdataDic in mryfArray)
            {
                NewsModel_yy *newModel = [NewsModel_yy yy_modelWithJSON:mryfdataDic];
                [_mryfDataModelArray addObject:newModel];
            }
            
            for(NSDictionary *mrytdataDic in mrytArray)
            {
                NewsModel_yy *newModel = [NewsModel_yy yy_modelWithJSON:mrytdataDic];
                [_mrytDataModelArray addObject:newModel];
            }
            
            for(NSDictionary *mryxdataDic in mryxArray)
            {
                NewsModel_yy *newModel = [NewsModel_yy yy_modelWithJSON:mryxdataDic];
                [_mryxDataModelArray addObject:newModel];
                
            }
            
            _allDataModelArray = [NSMutableArray arrayWithObjects:_mryxDataModelArray, _mrytDataModelArray, _mryfDataModelArray, nil];
        }
        
        if ([response isEqualToString:@"error"]) {
            NSDictionary *errorDic = [result objectForKey:@"error"];
            NSString *text = errorDic[@"text"];
            [Config showHUDWithText:text andImage:@"error"];
        }
    } failureAction:^{
        [Config showHUDWithText:@"数据获取错误" andImage:@"error"];
    }];
}

@end
